package com.mocne.TestCases;

import com.mocne.Common.Excel2Dataprovider;
import com.mocne.Common.PKFTestReport;
import com.mocne.Common.OperateWithParams;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;


@Listeners({PKFTestReport.class})
public class InterfaceTestTestNG_TestCase {

    Jedis jedis = new Jedis("localhost");
    WebDriver webDriver;

    @Test(dataProvider = "dataProvider")
    public void interfaceTest(HashMap<String, String> data) {
        OperateWithParams.operateWithParams(webDriver, data);
        System.out.println(data.toString());
    }


    @BeforeMethod
    public void beforeMethod() {
    }

    @AfterMethod
    public void afterMethod() {
    }

    @DataProvider(name="dataProvider")
    public Object[][] CasesDataProvider() {
        Object[][] dataProvider = new Object[][]{};

        try{
            Excel2Dataprovider e2d = new Excel2Dataprovider(jedis.get("dpFile"), jedis.get("caseSheet"));
            dataProvider = e2d.getExcelData();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return dataProvider;

    }


    @BeforeClass
    public void beforeClass() {
        System.out.println("beforeClass");
        System.setProperty("webdriver.chrome.driver", "/Users/pengkaifeng/BrowserDrivers/chromedriver");// chromedriver服务地址
        ChromeOptions options = new ChromeOptions();
        // 设置禁止加载项
        Map<String, Object> prefs = new HashMap<>();
/*
        prefs.put("profile.default_content_settings.javascript", 2); // 2就是代表禁止加载的意思
        prefs.put("profile.default_content_settings.images", 2); // 2就是代表禁止加载的意思
*/
        prefs.put("disable-infobars", 2);

        //关闭使用ChromeDriver打开浏览器时上部提示语"Chrome正在受到自动软件的控制"
        options.addArguments("disable-infobars");

        //加载浏览器的静默模式，使浏览器在后台运行
//        options.addArguments("headless");

        options.setExperimentalOption("prefs", prefs);


        webDriver = new ChromeDriver(options);
    }

    @AfterClass
    public void afterClass() {
        webDriver.close();
    }

    @BeforeTest
    public void beforeTest() {
    }

    @AfterTest
    public void afterTest() {
    }

    @BeforeSuite
    public void beforeSuite() {
    }

    @AfterSuite
    public void afterSuite() {
    }

    /**
     * @param str 需要校验的字符串
     * @return 包含的数字
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        return pattern.matcher(str).matches();
    }
}
